package com.jml.mybatis插件;


import java.util.Properties;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.plugin.Plugin;


@Intercepts({@Signature(type = Executor.class, method = "query",
args = {MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})})
public class MybatisPlugin  implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		//这个和上面的注解参数对应，
		// [0]就是MappedStatement，[1]就是Object
		//[2]就是RowBounds，[3]就是ResultHandler
		MappedStatement mappedStatement = (MappedStatement)invocation.getArgs()[0];
		BoundSql boundSql = mappedStatement.getBoundSql(invocation.getArgs()[1]);
		//%s就是参数，boundSql.getSql()有多少个数就要有多少个%s
		System.out.println(String.format("jml plugin output sql = %s ,param = ",boundSql.getSql()));
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target,this);
	}

	@Override
	public void setProperties(Properties properties) {

	}
}
